package ywh.list
{
	import ywh.event.MediaListEvent;
	import ywh.manager.Core;
	import ywh.manager.Tag;
	import ywh.utils.DataLoader;
	import ywh.utils.XMLUtils;
	
	/**
	 * 媒体列表类;
	 * @author 杨伟华
	 * @version 1.0
	 *
	 * Created 1/21/2013 3:45 PM
	 */
	public class MediaList
	{
		protected var _courseItems:Vector.<XML>;
		protected var _tempItem:CourseItem;
		
		protected var _totalDuration:Number
		protected var _xml:XML;
		
		public function MediaList()
		{
			_tempItem = new CourseItem();
		}
		
		/**
		 * 返回列表长度，共有多少元素;
		 */
		public function get count():uint
		{
			return _courseItems.length;
		}
		
		public function get xml():XML
		{
			return _xml;
		}
		
		public function get totalDuration():Number
		{
			return _totalDuration;
		}
		
		/**
		 * 加载列表;
		 * @param	url
		 */
		public function loadList(url:String):void
		{
			destory();
			_courseItems = new Vector.<XML>();
			dispatchEvent(MediaListEvent.LIST_LOADING);
			DataLoader.addData(url, parseMediaList, this);
		}
		
		/**
		 * 清空列表;
		 */
		public function destory():void
		{
			_xml = null;
			_courseItems = null;
			_totalDuration = 0;
		}
		
		public function getItemAt(index:uint):XML
		{
			return _courseItems[index];
		}
		
		/**
		 * 获得指定编号Item的起始时间;
		 * @param	index
		 * @return
		 */
		public function getItemStartTime(index:int):Number
		{
			if (isAvilableIndex(index))
			{
				_tempItem.data = _courseItems[index];
				return _tempItem.startTime;
			}
			return NaN;
		}
		
		/**
		 * 查询一个XML对应的节点编号;
		 */
		public function getIndexByItem(item:XML):int
		{
			return _courseItems.indexOf(item);
		}
		
		/**
		 * 获得起始时间在给定时间之前的项目编号;
		 * @param	value
		 * @return
		 */
		public function getIndexByStartTime(value:Number):int
		{
			return value > totalDuration ? -1 : getIndexByStartTimeInIndexScope(value, 0, count - 1);
		}
		
		/**
		 * 编号是否有效;
		 * @param	index
		 * @return
		 */
		public function isAvilableIndex(index:int):Boolean
		{
			return index >= 0 && index < count;
		}
		
		// 本类特异性私有方法整理数据;
		protected function parseMediaList(data:String, object = null):void
		{
			_xml = new XML(data);
			//_currentItem.defaultData = _xml;
			initCourseItemsList();
			dispatchEvent(MediaListEvent.LIST_READY);
		}
		
		/**
		 * 整理数据初始化_CourseItemsList及 _amount;
		 */
		protected function initCourseItemsList():void
		{
			(_xml.descendants(Tag.ITEM)).(parseXML(valueOf()));
		}
		
		/**
		 * 检测加入列表;
		 * @param	xml
		 * @return
		 */
		protected function parseXML(xml:XML):Boolean
		{
			return true;
		}
		
		private function dispatchEvent(type:String):void
		{
			Core.dispatchEvent(new MediaListEvent(type, this));
		}
		
		/**
		 * 获得起始时间在给定时间之前的项目编号;
		 * @param	value
		 * @param	startIndex
		 * @param	endIndex
		 * @return
		 */
		protected function getIndexByStartTimeInIndexScope(value:Number, startIndex:int, endIndex:int):int
		{
			var prop:String = Tag._START_TIME;
			var tempIndex:int;
			var tempValue:Number;
			var middleIndex:uint = Math.floor((startIndex + endIndex + 1) / 2);
			var middleItem:XML = _courseItems[middleIndex] as XML;
			var middleValue:Number = XMLUtils.getPropNumber(middleItem, prop);
			if (middleValue == value)
			{
				tempIndex = middleIndex;
			}
			else
			{
				if (middleValue > value)
				{
					endIndex = middleIndex - 1;
				}
				else
				{
					startIndex = middleIndex;
				}
				if (endIndex == startIndex)
				{
					tempIndex = startIndex;
				}
				else
				{
					return getIndexByStartTimeInIndexScope(value, startIndex, endIndex);
				}
			}
			if (true)
			{
				var pv:Number;
				var toMin:int = -1;
				tempValue = XMLUtils.getPropNumber(_courseItems[tempIndex], prop);
				tempIndex += toMin;
				for (; tempIndex > 0; tempIndex += toMin)
				{
					pv = XMLUtils.getPropNumber(_courseItems[tempIndex], prop);
					if (tempValue != pv)
					{
						break;
					}
				}
				tempIndex -= toMin;
			}
			return tempIndex;
		}
	}
}